package com.vending.management.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.vending.common.utils.DateUtils;
import com.vending.common.utils.uuid.IdUtils;
import com.vending.management.domain.Channel;
import com.vending.management.domain.Node;
import com.vending.management.domain.VmType;
import com.vending.management.mapper.ChannelMapper;
import com.vending.management.mapper.NodeMapper;
import com.vending.management.mapper.VmTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vending.management.mapper.VendingMachineMapper;
import com.vending.management.domain.VendingMachine;
import com.vending.management.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-28
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */

    @Transactional
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //1.新增设备
        //1-1 生成8位内的设备编号
        vendingMachine.setInnerCode(IdUtils.simpleUUID().substring(0, 8));
        //1-2 查询设备容量
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        //1-3 根据点位信息补充冗余字段
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        BeanUtil.copyProperties(node, vendingMachine,"id");
        vendingMachine.setBusinessType(node.getBusinessCircleType());
        vendingMachine.setAddr(node.getAddress());
        //1-4 设备状态 0 未投放
        vendingMachine.setVmStatus(0L);
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        //2.新增货道
        //2-1 双层for循环
        List<Channel> channelList = new ArrayList<>();
        for (int i = 1; i <= vmType.getVmRow(); i++) {
            for (int j = 1; j <= vmType.getVmCol(); j++) {
                Channel channel = new Channel();
                channel.setVmId(vendingMachine.getId());
                channel.setChannelCode(i + "-" + j);
                channel.setVmId(vendingMachine.getId());
                channel.setInnerCode(vendingMachine.getInnerCode());
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                channelList.add(channel);
            }
        }
        //2-2 批量新增货道
        channelMapper.batchInsertChannel(channelList);
        return result ;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        if (vendingMachine.getNodeId() != null) {
            Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
            BeanUtil.copyProperties(node, vendingMachine,"id","createTime");
            vendingMachine.setBusinessType(node.getBusinessCircleType());
            vendingMachine.setAddr(node.getAddress());
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }
}
